<template>
  <!-- 解决数据还没到来之前渲染undefined问题 -->
  <view class="goodsDetail" v-if="goods_info.goods_name">
    <!-- 轮播图区域 -->
    <swiper indicator-dots="true" autoplay="true" indicator-active-color="#fff" interval="3000" circular='true'>
      <swiper-item @click="prewImg(i)" v-for="(item,i) in goods_info.pics" :key="i">
        <image :src="item.pics_big" mode=""></image>
      </swiper-item>
    </swiper>
    <!-- 商品信息区域 -->
    <view class="goodsInfo">
      <view class="goods-price">
        ￥{{goods_info.goods_price}}
      </view>
      <view class="goodsBox">
        <view class="goods-name">
          {{goods_info.goods_name}}
        </view>
        <view class="star">
          <uni-icons type="star" size="20"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <view class="yy">
        <text>快递 : 免运费{{cart.length}}</text>
      </view>
    </view>
    <rich-text :nodes="goods_info.goods_introduce"></rich-text>
    <!-- 商品导航组件区域 -->
    <view class="goods-nav">
      <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
        @buttonClick="buttonClick" />
    </view>
  </view>
</template>

<script>
  import {
    mapState,mapMutations,mapGetters
  } from 'vuex'
  export default {
    computed: {
      ...mapState('m_cart', ['cart']),
      ...mapGetters('m_cart',['total'])
    },
    watch:{
      total:{
        immediate:true,
       handler(newInfo){
       let result=this.options.find(x=>x.text==='购物车');
       result.info=newInfo
         }, 
      }  
    },
    data() {
      return {
        // 页面数据
        goods_info: {},
        // 购物车数据
        options: [{
          icon: 'shop',
          text: '店铺',
        }, {
          icon: 'cart',
          text: '购物车',
          info: 0
        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]
      };
    },
    onLoad(option) {
      // console.log(option);
      this.getGoodsInfo(option.goods_id)
    },
    methods: {
      ...mapMutations('m_cart',['addToCart']),
      // 获取商品数据
      async getGoodsInfo(goods_id) {
        const {
          data
        } = await uni.$http.get('/api/public/v1/goods/detail', {
          goods_id
        })
        if (data.meta.status !== 200) return uni.$showMsg();
        // 解决图片之间存在缝隙的问题;及webp图片在ios上兼容性不好的问题
        data.message.goods_introduce = data.message.goods_introduce.replace(/<img /g, '<img style="display:block;"')
          .replace(/webp/g, 'jpg');
        this.goods_info = data.message;
      },
      // 预览轮播图图片
      prewImg(index) {
        uni.previewImage({
          // 预览时，默认显示图片的索引
          current: index,
          // 所有图片url地址的数组
          urls: this.goods_info.pics.map(img => img.pics_big)
        })
      },
      // 判断点击了左边哪个
      onClick(e) {
        if (e.content.text === "购物车") {
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
        // console.log(e);
      },
      // 判断点击了右边的哪个
      buttonClick(e) {
        // console.log(e);
        if (e.content.text === "加入购物车") {
          let goods = {
            goods_id:this.goods_info.goods_id,
            goods_price:this.goods_info.goods_price,
            goods_name:this.goods_info.goods_name,
            goods_count:1,
            goods_small_logo:this.goods_info.goods_small_logo,
            goods_state:true,
          }
            this.addToCart(goods)
        }
      
      }
    }
  }
</script>

<style lang="scss">
  swiper {
    height: 750rpx;

    image {
      width: 100%;
      height: 100%;
    }
  }

  .goodsInfo {
    padding: 10px;
    padding-right: 0;

    .goods-price {
      color: #c00000;
      font-size: 18px;
      margin: 10px 0;
    }

    .goodsBox {
      display: flex;
      justify-content: space-between;

      .goods-name {
        font-size: 13px;
        margin-right: 10px;
      }

      .star {
        width: 120px;
        font-size: 12px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        border-left: 1px solid #efefef;
        color: gray;
      }
    }

    .yy {
      font-size: 12px;
      color: gray;
      margin: 10px 0;
    }
  }

  .goods-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
  }

  .goodsDetail {
    padding-bottom: 50px;
  }
</style>
